/* 2017-12-4 15:15:45 | 修改 木遥（微信:  http://marsgis.cn/weixin.html ） */
$(document).ready(function () {
  initPage()
  bindEvents()
  // sidebarScrollFix()
})

let aceEditor
let containExamples = true

function initPage() {
  initEditor()
  screenResize()
}

function screenResize() {
  window.onresize = function () {
    mapHeight()
  }
}

function id2FileName(id) {
  let fileName = id + '.html'
  return fileName
}

function getConfig(locationParam, search) {
	let href = window.location.pathname
	let mapUrl = href.substr(0, href.lastIndexOf('/') + 1)
	mapUrl = mapUrl + "../json/index.json"
	var json = $.ajax({
	  url: mapUrl,
	  async: false,
	  dataType:'json',
	  error: function (error) {
	    haoutil.msg('获取配置文件出错！')
	    html = ''
	  },
	}).responseJSON
	return json;
}

function searchPage(json,key){
	let obj=null;
	for(var i=0;i<json.length;i++){
		let page=json[i];
		if(page.key==key){
			obj=page;
			break;
		}else{
			if(page.children && page.children.length>0){
				obj=searchPage(page.children,key);
				if(obj!=null){
					break;
				}
			}
		}
	}
	return obj;
}




//初始化编辑器
function initCodeEditor() {
  if (!aceEditor) {
    aceEditor = ace.edit('editor')
    aceEditor.setTheme('ace/theme/xcode')
    aceEditor.getSession().setMode('ace/mode/html')
    aceEditor.getSession().setUseWrapMode(true)
    aceEditor.setShowPrintMargin(false)
    aceEditor.$blockScrolling = Infinity
  }
  aceEditor.setValue($('#editor').val())
  aceEditor.clearSelection()
  aceEditor.moveCursorTo(0, 0)
}

//初始化编辑器以及预览内容
function initEditor() {
  loadExampleHtml()
  initCodeEditor()
}

function loadExampleHtml() {
  let locationParam = getLocationParam()
  if (!locationParam) {
    return
  }

  let config=getConfig(locationParam, window.location.search)
 
  if(config==null){
	   haoutil.msg('该页面不存在，请检查地址！');
	   return;
  }
  
  let page=searchPage(config,locationParam);
  if(page==null){
  	   haoutil.msg('该页面不存在，请检查地址！');
  	   return;
  }
  
  document.title = page.name
  
  let href = window.location.pathname
  let mapUrl = href.substr(0, href.lastIndexOf('/') + 1)
  mapUrl = mapUrl + page.url;
  if (!mapUrl) {
    return
  }

  let cacheVersion = '20210501'
  if (mapUrl.indexOf('?') == -1) {
    mapUrl += '?time=' + cacheVersion
  } else if (mapUrl.indexOf('time=' + cacheVersion) == -1) {
    mapUrl += '&time=' + cacheVersion
  }
  var html = $.ajax({
    url: mapUrl,
    async: false,
    error: function (error) {
      haoutil.msg('该页面不存在，请检查地址！')
      html = ''
    },
  }).responseText
  if (html && html != '') {
    html = html.replace(new RegExp(/[\s]*<meta[^>]*?\/?>/, 'gm'), '')
    $('#editor').val(html)
    loadPreview(html)
  }
}

function getLocationParam() {
  let param = window.location.toString()
  if (param.indexOf('#') === -1) {
    return '11_online_tdt'
  }
  param = param.split('#')
  if (param && param.length > 0) {
    return param[1]
  }
}

//运行代码
function run() {
  let iframeContent = $('#editor').val()
  if (editor) {
    iframeContent = aceEditor.getValue()
  }
  loadPreview(iframeContent)
}

//填充预览效果内容
function loadPreview(content) {
  let iFrame = createIFrame(),
    iframeDocument = iFrame.contentWindow.document

  iframeDocument.open()
  iframeDocument.write(content)
  iframeDocument.close()

  let doc = document
  iFrame.addEventListener('load', function () {
    mapHeight()
    //setTimeout(function () {
    //    doc.title = iframeDocument.title;
    //}, 100);
  })
  mapHeight()
}

function loadIFrameForSrc(url) {
  createIFrame()
  $('#innerPage').attr('src', url)

  mapHeight()
}

function createIFrame() {
  let preViewPane = $('#previewPane')
  preViewPane.empty()
  let iframe = document.createElement('iframe')
  $(iframe).attr('id', 'innerPage')
  $(iframe).attr('name', 'innerPage')
  preViewPane.append(iframe)
  return iframe
}

//重置编辑器
function refresh() {
  initEditor()
  run()
}

// function initSelect() {
//   let hash = window.location.hash
//   let id
//   if (hash.indexOf('#') === -1) {
//     id = $('section#sidebar .thirdMenu a.link').first().attr('id')
//     window.location.hash = id ? '#' + id : window.location.hash
//   } else {
//     id = hash.split('#')[1]
//   }
//   selectMenu(id)
// }

function mapHeight() {
  let doc = $('#innerPage').contents()
  doc.find('html').height('100%')
  doc.find('body').height('100%')
}

function bindEvents() {
  $('#sidebar ul.third-menu a').click(function (evt) {
    let target = $(evt.target).parent().parent()
    let nodeId = evt.target.id
    //如果点击的是span节点还要往上一层
    if (evt.target.localName === 'span') {
      nodeId = target.attr('id')
    }

    if (nodeId) {
      //阻止冒泡防止上层事件响应导致修改url hash值
      evt.preventDefault()
      window.location.hash = '#' + nodeId
      initEditor()
      evt.stopPropagation()
    }
  })
  let codePane = $('#codePane')
  let previewPane = $('#previewPane')
  let expand = !!1
  $('#showCodeBtn').click(function () {
    if (expand) {
      //编辑器和预览宽度5:7
      $(this).text(' 收缩').css({ left: '500px' })
      $(this).addClass(' fa-compress')
      $(this).removeClass('fa-arrows-alt')
      codePane.show(10, function () {
        previewPane.removeClass('col-md-12')
        previewPane.addClass('col-md-7')
        codePane.addClass('col-md-5')

        $('#showCodeBtn').css({ left: $('#codePane').width() + 5 + 'px' })
        if (aceEditor) {
          aceEditor.resize()
        }
      })
    } else {
      //预览独占一行
      $(this).text(' 源码').css({ left: '0px' })
      $(this).addClass('fa-arrows-alt')
      $(this).removeClass(' fa-compress')
      codePane.hide(200, function () {
        codePane.removeClass('col-md-5')
        previewPane.removeClass('col-md-7')
        previewPane.addClass('col-md-12')
      })
    }
    expand = !expand
  })

  if (haoutil.system.getRequestByName('code')) {
    $('#showCodeBtn').click()
  }

  // window.addEventListener('hashchange', function () {
  //   let hash = window.location.hash
  //   if (hash.indexOf('#') !== -1) {
  //     let id = hash.split('#')[1]
  //     selectMenu(id)
  //   }
  // })
}
